package com.zhonggong.network.emp.service.impl;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.zhonggong.network.emp.dao.EmpDao;
import com.zhonggong.network.emp.dao.impl.EmpDaoImpl;
import com.zhonggong.network.emp.model.Emp;
import com.zhonggong.network.emp.service.EmpManager;



public class EmpManagerImpl implements EmpManager{
	
	protected Logger logger = LogManager.getLogger(this.getClass().getName());

	List<Emp> empList;
	EmpDao empDao = new EmpDaoImpl();
	
	public void load(String fileName) throws FileNotFoundException {
		// TODO Auto-generated method stub
		
		this.empList = new ArrayList<Emp>();
		File file = new File(fileName);
		InputStream input = new FileInputStream(file);
		XSSFWorkbook wb = null;
		try {
			wb = new XSSFWorkbook(input);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		XSSFSheet sheet = wb.getSheetAt(1);
		for (int i = 1; i <= 5; i++) {
			Row row = sheet.getRow(i);
			
			Emp e = new Emp();
			e.setId(Long.valueOf(1000 + i));
			for (int j = 0; j < 3; j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					cell.setCellType(CellType.STRING);
					String value = String.valueOf(row.getCell(j).getStringCellValue());
					if(value == null)
					{
						logger.info("value is null");
						return ;
					}
					if (j == 0)
						e.setName(value);
					if (j == 1)
						e.setGongzi(value);
					if (j == 2)
						e.setBumen(value);
				}
			}
			logger.info(" emp_" + i + " is :" + e);
			new EmpDaoImpl().create(e);
			this.empList.add(e);
		}
	
	}

	public void create(Emp emp) {
		// TODO Auto-generated method stub
		empDao.create(emp);
	}

	public int remove(Emp emp) {
		// TODO Auto-generated method stub
		return empDao.remove(emp);
	}

	public void update(Emp emp) {
		// TODO Auto-generated method stub
		empDao.update(emp);
	}

	public Emp findById(Long id) {
		// TODO Auto-generated method stub
		return empDao.findById(id);
	}

	public List<Emp> findByName(String name) {
		// TODO Auto-generated method stub
		return empDao.findByName(name);
	}
	
}
